package io.bluerain.asnycimage.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import io.bluerain.asnycimage.R;

/**
 * Created by foredawn on 15-10-3.
 * 异步图片加载的GridView示例
 */
public class GridActivity extends Activity {

    class Item {
        private String text;
        private String imageUrl;

        public Item(String text, String imageUrl) {
            this.text = text;
            this.imageUrl = imageUrl;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        @Override
        public String toString() {
            return "Item{" +
                    "text='" + text + '\'' +
                    ", imageUrl='" + imageUrl + '\'' +
                    '}';
        }
    }

    private ImageLoader loader;
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.async_gridview);
        initImageLoader();

        Item[] items = new Item[]{
                new Item("百度", "https://www.baidu.com/img/bdlogo.png"),
                new Item("腾讯", "http://www.easyicon.net/api/resizeApi.php?id=511337&size=128"),
                new Item("谷歌", "http://www.easyicon.net/api/resizeApi.php?id=1183734&size=128"),
                new Item("贴吧", "http://www.easyicon.net/api/resizeApi.php?id=1188647&size=128")
        };

        ListGridItemAdapter adapter = new ListGridItemAdapter(items);
        GridView gridView = (GridView) findViewById(R.id.grid_view);
        gridView.setAdapter(adapter);
    }

    //自定义adapter
    class ListGridItemAdapter extends BaseAdapter {
        private Item[] items;

        public ListGridItemAdapter(Item[] items) {
            this.items = items;
        }

        @Override
        public int getCount() {
            return this.items.length;
        }

        @Override
        public Object getItem(int position) {
            return items[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.grid_item, parent, false);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.img_item);
                holder.text = (TextView) convertView.findViewById(R.id.txt_item);
                //设置tag
                convertView.setTag(holder);
                //设置内容
                Item item = (Item) getItem(position);
                holder.text.setText(item.getText());
                //异步显示每一张图片（使用同一种options）
                loader.displayImage(item.getImageUrl(), holder.image, options);
            }

            return convertView;
        }

        class ViewHolder {
            public ImageView image;
            public TextView text;
        }
    }

    private void initImageLoader() {

        //初始化操作
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .build();
        ImageLoader.getInstance().init(config);

        //获取Loader实例
        loader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading) //图片下载时显示的内容
                .build();
    }
}
